package com.fjeld.client.gui.screens.node;

import com.fjeld.swingx.*;
import com.fjeld.middleware.jaxb.V1Node;
import com.fjeld.middleware.constants.FontConstants;
import com.fjeld.client.gui.util.factories.LabelFactory;
import com.fjeld.client.util.cache.ClientCacheManager;
import com.explodingpixels.macwidgets.HudWidgetFactory;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Observer;
import java.util.Observable;

import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: fjelds
 * Date: Mar 24, 2010
 * Time: 9:23:36 PM
 */
public class ActionCommandsView extends JContentRenderingPanel implements ActionListener, Observer
{
    private static final Logger logger = Logger.getLogger(ActionCommandsView.class);
    private static final ClientCacheManager CCM = ClientCacheManager.getInstance();
    private JTextArea area;
    private JRoundedButton saveAction;
    private V1Node action;
    private JLabel nodeRefIDLabel;
    private JTextField hostname;
    private JTextField username;
    private JTextField password;

    public ActionCommandsView()
    {
        JLabel label = LabelFactory.createHeaderLargeLabelNonPadded("Action commands:");
        nodeRefIDLabel = LabelFactory.createHeaderLabelNonPadded("");
        createTextArea();

        Box b = Box.createVerticalBox();
        b.add(label);
        b.add(nodeRefIDLabel);
        add(b, BorderLayout.NORTH);

        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setPreferredSize(new Dimension(100, 200));

        saveAction = new JRoundedButton("Save");
        saveAction.addActionListener(this);

        JTransparentPanel p = new JTransparentPanel();
        p.add(saveAction, BorderLayout.WEST);
        add(p, BorderLayout.SOUTH);

    }

    private void createTextArea()
    {
        area = new JTextArea();
        area.setCaretColor(ColorConstants.TEXT_NORMAL);
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        style(area);
        area.setBorder(null);

        JTransparentPanel p = new JTransparentPanel();
        Border emptyBorder = BorderFactory.createEmptyBorder(10,10,10,10);
        Border innerEmptyBorder = BorderFactory.createLineBorder(ColorConstants.STANDARD_BACKGROUND, 2);
        Border roundedBorder = BorderFactory.createLineBorder(ColorConstants.TEXT_HIGHLIGHT_WHITE, 1);
        Border compoundBorder = BorderFactory.createCompoundBorder(emptyBorder, roundedBorder);
        Border compoundBorder2 = BorderFactory.createCompoundBorder(compoundBorder, innerEmptyBorder);
        p.setBorder(compoundBorder2);
        p.add(area, BorderLayout.CENTER);

        JTransparentPanel p2 = new JTransparentPanel();
        Border emptyBorder2 = BorderFactory.createEmptyBorder(10,10,10,10);
        p2.setBorder(emptyBorder2);

        password = HudWidgetFactory.createHudTextField("Password");
        username = HudWidgetFactory.createHudTextField("Username");
        hostname = HudWidgetFactory.createHudTextField("Hostname");
        style(password);
        style(username);
        style(hostname);

        Box box = Box.createVerticalBox();
        box.add(hostname);
        box.add(Box.createVerticalStrut(5));
        box.add(username);
        box.add(Box.createVerticalStrut(5));
        box.add(password);
        p2.add(box, BorderLayout.NORTH);

        JTransparentPanel content = new JTransparentPanel();
        content.add(p2, BorderLayout.NORTH);
        content.add(p, BorderLayout.CENTER);

        add(content, BorderLayout.CENTER);
    }

    private void style(JTextComponent textComponent)
    {
        Border existingBorder = BorderFactory.createLineBorder(ColorConstants.TEXT_HIGHLIGHT_WHITE, 1);
        TitledBorder titledBorder = BorderFactory.createTitledBorder(existingBorder, textComponent.getText());
        titledBorder.setTitlePosition(TitledBorder.LEFT);
        titledBorder.setTitleColor(ColorConstants.TEXT_HIGHLIGHT_WHITE);
        titledBorder.setTitleFont(FontConstants.PLAIN_FONT);

        textComponent.setCaretColor(ColorConstants.TEXT_NORMAL);
        textComponent.setForeground(ColorConstants.TEXT_HIGHLIGHT_WHITE);
        textComponent.setBackground(ColorConstants.STANDARD_BACKGROUND);
        textComponent.setFont(FontConstants.PLAIN_FONT);
        textComponent.setOpaque(true);
        textComponent.setBorder(titledBorder);
    }

    public void setCommands(V1Node action)
    {
        if (this.action != null)
            this.action.deleteObserver(this);

        this.action = action;
        action.addObserver(this);
        area.setText(action.getV1Content());
        hostname.setText(action.getHostname());
        username.setText(action.getUsername());
        password.setText(action.getPassword());
        nodeRefIDLabel.setText(action.getNodeID());
        repaint();
    }

    public void actionPerformed(ActionEvent actionEvent)
    {
        CCM.setContent(action.getNodeID(), area.getText(), hostname.getText(), username.getText(), 
                password.getText());
    }

    public void update(Observable observable, Object o)
    {
        String newCommands = action.getV1Content();
        String existingCommands = area.getText();

        if ((existingCommands == null && newCommands == null) || !newCommands.equals(existingCommands))
        {
            area.setText(action.getV1Content());
            hostname.setText(action.getHostname());
            username.setText(action.getUsername());
            password.setText(action.getPassword());

            revalidate();
            repaint();
        }
    }
}